<?php
namespace Admin\Controller;
use Tool\AdminController;

class ActicleController extends AdminController {
	function acticle() {
		$acticle = D('Acticle');
		$info = $acticle->select();
		$this->assign('info',$info);
		
		$total = $acticle->count();
		$page = new \Think\Page($total,10);
		$page->setConfig('header', '条记录');
		$page->setConfig('prev', '上一页');
		$page->setConfig('next', '下一页');
		$page->setConfig('first', '第一页');
		$page->setConfig('end', '最末页');
		$page->setConfig('theme', '总共%TOTAL_ROW% %HEADER%，共%TOTAL_PAGE%页，当前第%NOW_PAGE%页|%FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END% ');
		$show = $page->show();
		$this->assign('page',$show);
		
		//批量删除
		if (!empty($_POST['checkbox'])) {
			$check = implode(',',$_POST['checkbox']);
			$info = $acticle->where('id ' . 'IN(' . $check . ')')->select();
			$z = $acticle->where('id ' . 'IN(' . $check . ')')->delete();
			if ($z) {
				$this->redirect('Tips/tips');
			}
		}

		include ('top.php');
		$this->display();
	}
	
	function add() {
		$qu = new \Model\QuestionModel();
		$acticle = D('Acticle');
		if (!empty($_POST)) {
			if (!empty($_POST['content'])) {
				$z = $qu->writefile('Upload/bulletin/', $_POST['content']);
				if ($z) {
					$_POST['content'] = $z;
				}
			}
			$_POST['add_time'] = strtotime(date('Y-m-d H:i:s'));
			$info = $acticle->add($_POST);
			if ($info) {
				$this->redirect('Tips/tips');
			}
		}

		include ('top.php');
		$this->display();
	}

	function upd() {
		$qu = new \Model\QuestionModel();
		$acticle = D('Acticle');
		$info = $acticle->where('id=' . $_GET['id'])->find();
		$this->assign('info',$info);
		
		if (!empty($_POST)) {
			if (!empty($_POST['content'])) {
				unlink($info['content']);
				$z = $qu->writefile('Upload/bulletin/', $_POST['content']);
				if ($z) {
					$_POST['content'] = $z;
				}
			}
			$_POST['add_time'] = strtotime(date('Y-m-d H:i:s'));
			$info = $acticle->where('id=' . $_GET['id'])->save($_POST);
			if ($info) {
				$this->redirect('Tips/tips');
			}
		}

		include ('top.php');
		$this->display();
	}
	
	function del() {
		$acticle = D('Acticle');
		$info = $acticle->where('id=' . $_GET['id'])->find();
		unlink($info['content']);
		$z = $acticle->where('id=' . $_GET['id'])->delete();
		if ($z) {
			$this->redirect('Tips/tips');
		}
	}
}